//-----------------------------------------------------------------------
// <copyright file="CertificateDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CertificateDAOBase
    {
        #region Common methods
        public virtual Certificate CreateCertificateFromReader(IDataReader reader)
        {
            Certificate item = new Certificate();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CertificateID"))) item.CertificateID = (int)reader["CertificateID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOder"))) item.SortOder = (int)reader["SortOder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCertificateFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCertificate methods
            
        public virtual void CreateCertificate(Certificate certificate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCertificateInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, certificate.Title);
                database.AddInParameter(dbCommand, "@SortOder", DbType.Int32, certificate.SortOder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, certificate.Enable);
                database.AddOutParameter(dbCommand, "@CertificateID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                certificate.CertificateID = (int)database.GetParameterValue(dbCommand, "@CertificateID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCertificateException, ex);
            }
        }

        #endregion

        #region UpdateCertificate methods
        
        public virtual void UpdateCertificate(Certificate certificate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCertificateUpdate");            
                
                database.AddInParameter(dbCommand, "@CertificateID", DbType.Int32, certificate.CertificateID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, certificate.Title);
                database.AddInParameter(dbCommand, "@SortOder", DbType.Int32, certificate.SortOder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, certificate.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCertificateException, ex);
            }
        }
        
        #endregion

        #region DeleteCertificate methods
        public virtual void DeleteCertificate(int certificateID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCertificateDelete");
                
                database.AddInParameter(dbCommand, "@CertificateID", DbType.Int32, certificateID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCertificateException, ex);
            }
        }

        #endregion

        #region GetCertificate methods
        
        public virtual Certificate GetCertificate(int certificateID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCertificateGet");
                
                database.AddInParameter(dbCommand, "@CertificateID", DbType.Int32, certificateID);
                
                Certificate certificate =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        certificate = CreateCertificateFromReader(reader);
                        reader.Close();
                    }
                }
                return certificate;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCertificateException, ex);
            }
        }
        
        #endregion

        #region GetCertificateList methods
        public virtual CertificateCollection GetCertificateList(CertificateColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCertificateGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CertificateCollection certificateCollection = new CertificateCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Certificate certificate = CreateCertificateFromReader(reader);
                        certificateCollection.Add(certificate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return certificateCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCertificateListException, ex);
            }
        }
        
        public virtual CertificateCollection GetCertificateList(CertificateColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCertificateList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCertificateList methods
        public virtual CertificateCollection GetCertificateListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCertificateGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CertificateCollection certificateCollection = new CertificateCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Certificate certificate = CreateCertificateFromReader(reader);
                        certificateCollection.Add(certificate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return certificateCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCertificateListException, ex);
            }
        }
        
        public virtual CertificateCollection GetCertificateListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCertificateListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}